<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
error_reporting(E_ERROR | E_PARSE );
function p($d,$die=true,$pre=true){
	header('Content-type:text/html; charset=utf-8');
	if ($pre) {echo "<pre />";}
    print_r($d);
    if ($die) {	die;}
}
/**
 * 获取类别组织
 * @param string $m [模型]
 * @param array  $f [字段列表]
 * @param string $c [过滤条件]
 * @param string $o [排序方式]
 */
function CAT($m='',$f=array(),$c='',$o='')
{
    vendor('Category');
    return new Qing\Category($m,$f,$c,$o);
}

/**
 * 获取首字母
 * @param  [type] $s0 [description]
 * @return [type]     [description]
 */
function getFirstchar($s0){ 
	$firstchar_ord=ord(strtoupper($s0{0})); 
	if (($firstchar_ord>=65 and $firstchar_ord<=91)or($firstchar_ord>=48 and $firstchar_ord<=57)) return $s0{0}; 
	$s=iconv("UTF-8","gb2312", $s0); 
	$asc=ord($s{0})*256+ord($s{1})-65536; 
	if($asc>=-20319 and $asc<=-20284)return "A"; 
	if($asc>=-20283 and $asc<=-19776)return "B"; 
	if($asc>=-19775 and $asc<=-19219)return "C"; 
	if($asc>=-19218 and $asc<=-18711)return "D"; 
	if($asc>=-18710 and $asc<=-18527)return "E"; 
	if($asc>=-18526 and $asc<=-18240)return "F"; 
	if($asc>=-18239 and $asc<=-17923)return "G"; 
	if($asc>=-17922 and $asc<=-17418)return "H"; 
	if($asc>=-17417 and $asc<=-16475)return "J"; 
	if($asc>=-16474 and $asc<=-16213)return "K"; 
	if($asc>=-16212 and $asc<=-15641)return "L"; 
	if($asc>=-15640 and $asc<=-15166)return "M"; 
	if($asc>=-15165 and $asc<=-14923)return "N"; 
	if($asc>=-14922 and $asc<=-14915)return "O"; 
	if($asc>=-14914 and $asc<=-14631)return "P"; 
	if($asc>=-14630 and $asc<=-14150)return "Q"; 
	if($asc>=-14149 and $asc<=-14091)return "R"; 
	if($asc>=-14090 and $asc<=-13319)return "S"; 
	if($asc>=-13318 and $asc<=-12839)return "T"; 
	if($asc>=-12838 and $asc<=-12557)return "W"; 
	if($asc>=-12556 and $asc<=-11848)return "X"; 
	if($asc>=-11847 and $asc<=-11056)return "Y"; 
	if($asc>=-11055 and $asc<=-10247)return "Z"; 
	return null;
}

/**
 * 获取给定时间的前几天或者后几天
 * @param  integer $num   天数
 * @param  string  $start 时间
 * @param  string  $fig   时间相加减
 * @return [type]         二维数组，键名为当天时间，包括当天时间的开始和结束
 */
function getDayMap($num=30,$start='',$fig='-')
{
	$start=$start?:time();
	$arr=array();
	for ($i=0; $i < $num; $i++) {
		$tmp=strtotime($fig.$i." day",$start);
		$year=date("Y",$tmp);$month=date("m",$tmp);$day=date("d",$tmp);
		$st=mktime(0,0,0,$month,$day,$year);
		$et=mktime(23,59,59,$month,$day,$year);
		$arr[$st]=array('st'=>$st,'et'=>$et);
	}
	ksort($arr);
	return $arr;
}

/**
 * 获取给定时间的中间时间
 * @param  [type] $stt 开始时间
 * @param  [type] $ett 结束时间
 * @return [type]      二维数组，键名为当天时间，包括当天时间的开始和结束
 */
function getDayMapSE($stt,$ett)
{
	$stt=getOneDay($stt);$stt=$stt['st'];
	$ett=getOneDay($ett);$ett=$ett['et'];
	$diff_day=intval(($ett-$stt)/(24*3600));
	$diff_arr=range(0,$diff_day);
	// array_shift($diff_arr);
	foreach ($diff_arr as $key => $value) {
		$tmp=strtotime($value." day",$stt);
		$year=date("Y",$tmp);$month=date("m",$tmp);$day=date("d",$tmp);
		$st=mktime(0,0,0,$month,$day,$year);
		$et=mktime(23,59,59,$month,$day,$year);
		// $arr[date('Y-m-d H:i:s',$st)]=array('st'=>date('Y-m-d H:i:s',$st),'et'=>date('Y-m-d H:i:s',$et));
		$arr[$st]=array('st'=>$st,'et'=>$et);
	}
	ksort($arr);
	return $arr;
}

/**
 * 获取给定时间当天的开始和结束时间
 * @param  string $d [时间]
 * @return [type]    [description]
 */
function getOneDay($d='')
{
	$d=$d?:time();
	$year=date("Y",$d);$month=date("m",$d);$day=date("d",$d);
	$st=mktime(0,0,0,$month,$day,$year);
	$et=mktime(23,59,59,$month,$day,$year);
	return array('st'=>$st,'et'=>$et);
}

/**
 * 通过给定时间和数量算出 开始时间和结束时间
 * @param  string  $time [时间]
 * @param  integer $num  [相差数量]
 * @param  string  $unit [单位，(day,)]
 * @param  string  $fig  [时间加减]
 * @return [type]        [数组 st开始时间 et结束时间]
 */
function getDayRegion($time='', $num=30, $unit='', $fig='-')
{
	$num=$num-1;
	$d=$d?:time();$unit=$unit?:'day';
	$tmp=strtotime($fig.$num." ".$unit,$d);
	$t1=getOneDay($tmp);$t2=getOneDay($d);
	if ($fig=='+') {
		$st=$t2['st'];$et=$t1['et'];
	}else{
		$st=$t1['st'];$et=$t2['et'];
	}
	return array('st'=>$st,'et'=>$et);
}

/**
 * 获取小程序 Accesstoken
 */
function Accesstoken(){
	$config = think\Config::get('wx.xcx');
    $xcx = new Wx\Xcx($config);
    return $xcx->access_token;
}

/**
	 * 模板ID
	frHLW0924vcVgMfiT-dm3S5URRvYxV2Sf2GdaK7vtxA
	标题
	订单支付成功通知
	关键词
	单号
	{{keyword1.DATA}}
	下单时间
	{{keyword2.DATA}}
	订单金额
	{{keyword3.DATA}}
	商品名称
	{{keyword4.DATA}}
	支付金额
	{{keyword5.DATA}}
	支付方式
	{{keyword6.DATA}}
	支付时间
	{{keyword7.DATA}}
 *  发送模板消息
 *  {
	  "touser": "OPENID",  
	  "template_id": "TEMPLATE_ID", 
	  "page": "index",          
	  "form_id": "FORMID",         
	  "data": {
	      "keyword1": {
	          "value": "339208499", 
	          "color": "#173177"
	      }, 
	      "keyword2": {
	          "value": "2015年01月05日 12:30", 
	          "color": "#173177"
	      }, 
	      "keyword3": {
	          "value": "粤海喜来登酒店", 
	          "color": "#173177"
	      } , 
	      "keyword4": {
	          "value": "广州市天河区天河路208号", 
	          "color": "#173177"
	      } 
	  },
	  "emphasis_keyword": "keyword1.DATA" 
	}
 * @return [type] [description]
 */
function sendPaytmp($d){
	$data = [
		'touser'      => $d['touser'],
		'template_id' => 'frHLW0924vcVgMfiT-dm3S5URRvYxV2Sf2GdaK7vtxA',
		'page'        => $d['page'],
		'form_id'     => $d['form_id'],
		'data'        => [
			'keyword1' => ['value'=>$d['data']['keyword1'], 'color'=>'#173177'],
			'keyword2' => ['value'=>$d['data']['keyword2'], 'color'=>'#173177'],
			'keyword3' => ['value'=>$d['data']['keyword3'], 'color'=>'#173177'],
			'keyword4' => ['value'=>$d['data']['keyword4'], 'color'=>'#173177'],
			'keyword5' => ['value'=>$d['data']['keyword5'], 'color'=>'#173177'],
			'keyword6' => ['value'=>$d['data']['keyword6'], 'color'=>'#173177'],
			'keyword7' => ['value'=>$d['data']['keyword7'], 'color'=>'#173177'],
		],
		'emphasis_keyword' => $d['emphasis_keyword']
	];

	$config = think\Config::get('wx.xcx');
    $xcx = new Wx\Xcx($config);
    $re = $xcx->sendTmp($data);
    if ($re) {
    	return true;
    }else{
    	return $xcx->getError();
    }
}

/**
 * 订单发货提醒
 * 模板ID
	FqDDl9pEXDAgOKaxpAxELXm4GfiqG-M4frqJoVqqogY
	标题
	订单发货提醒
	关键词
	订单号
	{{keyword1.DATA}}
	商品名称
	{{keyword2.DATA}}
	发货时间
	{{keyword3.DATA}}
	收货地址
	{{keyword4.DATA}}
	姓名
	{{keyword5.DATA}}
	电话
	{{keyword6.DATA}}
	配送方式
	{{keyword7.DATA}}
 * @param  [type] $d [description]
 * @return [type]    [description]
 */
function sendOrdertmp($d){
	$data = [
		'touser'      => $d['touser'],
		'template_id' => 'FqDDl9pEXDAgOKaxpAxELXm4GfiqG-M4frqJoVqqogY',
		'page'        => $d['page'],
		'form_id'     => $d['form_id'],
		'data'        => [
			'keyword1' => ['value'=>$d['data']['keyword1'], 'color'=>'#173177'],
			'keyword2' => ['value'=>$d['data']['keyword2'], 'color'=>'#173177'],
			'keyword3' => ['value'=>$d['data']['keyword3'], 'color'=>'#173177'],
			'keyword4' => ['value'=>$d['data']['keyword4'], 'color'=>'#173177'],
			'keyword5' => ['value'=>$d['data']['keyword5'], 'color'=>'#173177'],
			'keyword6' => ['value'=>$d['data']['keyword6'], 'color'=>'#173177'],
			'keyword7' => ['value'=>$d['data']['keyword7'], 'color'=>'#173177'],
		],
		'emphasis_keyword' => $d['emphasis_keyword']
	];

	$config = think\Config::get('wx.xcx');
    $xcx = new Wx\Xcx($config);
    $re = $xcx->sendTmp($data);
    if ($re) {
    	return true;
    }else{
    	return $xcx->getError();
    }
}